package cpr.command.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import cpr.DefaultCommandHandler;
import cpr.Printer;

public class CmdDbPrintTable extends DefaultCommandHandler {
	public CmdDbPrintTable() {
		super("db-print-table");
	}
	public void execute(String[] params) {
		// db-print-table jdbc:odbc:getit PCUSTOMER_2007_07_05
		// db-print-table jdbc:mysql://localhost/cpr01?user=cpr01user&password=cprpass PCUSTOMER_2007_07_05
		// db-print-table jdbc:odbc:DUSVLTCH [DUSVLTCH$]
		// db-print-table jdbc:odbc:PSALESORG [PSALESORG$]
		
		String jdbcString = getParameter(params,1); //"jdbc:mysql://localhost/cpr01?user=cpr01user&password=cprpass"
		String tableName = getParameter(params,2);

		Statement stmt = null;
		ResultSet rs = null;
	    Connection conn = null; 
	    try {
	    	Class.forName("com.mysql.jdbc.Driver");
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    	
		    conn = DriverManager.getConnection(jdbcString);
		    stmt = conn.createStatement();
		    rs = stmt.executeQuery("SELECT * FROM "+tableName);

		    ResultSetMetaData md = rs.getMetaData();
		    StringBuffer sb = new StringBuffer();
		    
	    	for (int i = 1; i <= md.getColumnCount(); i++) {
				String name = md.getColumnName(i);
		        sb.append(name+"\t");
			}
	    	Printer.println(sb.toString()+"\n-----");
	    	int count = 0;
	    	int max_count = 100;
		    while (rs.next()) {
		    	count++;
			    sb = new StringBuffer();
		    	for (int i = 1; i <= md.getColumnCount(); i++) {
					String name = md.getColumnName(i);
			        String value = rs.getString(name);
			        sb.append(value+"\t");
				}
		    	Printer.println(sb.toString());
		    	if (count>=max_count) break;
		    }
	    	Printer.println(""+count+" records found.");

		    // or alternatively, if you don't know ahead of time that
		    // the query will be a SELECT...

//		    if (stmt.execute("SELECT foo FROM bar")) {
//		        rs = stmt.getResultSet();
//		    }
		   

		    // Now do something with the ResultSet ....
		} catch (ClassNotFoundException ex) {
		    System.out.println("ClassNotFoundException: " + ex.getMessage());
			
		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		} finally {
		    // it is a good idea to release
		    // resources in a finally{} block
		    // in reverse-order of their creation
		    // if they are no-longer needed

		    if (rs != null) {
		        try {
		            rs.close();
		        } catch (SQLException sqlEx) { // ignore }

		        rs = null;
		    }

		    if (stmt != null) {
		        try {
		            stmt.close();
		        } catch (SQLException sqlEx) { // ignore }

		        stmt = null;
		    }
		}
		
		
		
    }
		}
	
}
}
